//
//  HelpAndFeedbackViewController.m
//  WE
//
//  Created by apple on 15/11/28.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "HelpAndFeedbackViewController.h"
#import "Size.h"
#import "BaseView.h"
#import "ToolUtil.h"
#import "HotProblemTableViewCell.h"
#import "ProblemDetailsViewController.h"
#import "CustomerServiceViewController.h"
@interface HelpAndFeedbackViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UISearchBar *searchBar1;
    UIVisualEffectView *view;
    UISegmentedControl *seg;
    NSArray *arr;
    BOOL flag;
    NSInteger i;
}
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation HelpAndFeedbackViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    i = 0;
    flag = YES;
    self.navigationItem.title = @"帮助与反馈";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
   seg = [[UISegmentedControl alloc] initWithItems:@[@"热门问题",@"问题分类"]];
    seg.frame = CGRectMake(WindowWidth/4, 70, WindowWidth/2, 30*Height);
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(changeSeg) forControlEvents:UIControlEventValueChanged];
    arr = @[@"注册与登录",@"账号相关",@"预约相关",@"就诊相关",@"其他产品",@"活动相关"];
    [self.view addSubview:seg];
    
    [ToolUtil createLine:70+39*Height superView:self.view lift:0 width:WindowWidth hight:1*Height color:GRAYCOLOR];
    searchBar1 = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 70+50*Height, WindowWidth - 10, 40)];
    searchBar1.barStyle = UIBarStyleDefault;
    searchBar1.placeholder = @"搜索医院";
    searchBar1.delegate = self;
    [self.view addSubview:searchBar1];
    searchBar1.searchBarStyle = UISearchBarStyleMinimal;
//    view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)];
//    view.hidden = YES;
//    view.alpha = 0.8;
    view = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    view.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
    view.hidden = YES;
    UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pressCancle)];
    doubleClick.numberOfTapsRequired = 1;
    [view addGestureRecognizer:doubleClick];
    [self.view addSubview:view];
//    view.backgroundColor = [UIColor colorWithRed:210.5/255. green:209.4/255. blue:211.6/255. alpha:1.0];
//    [view addTarget:self selector:@selector(pressCancle)];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(WindowWidth - 60, 69, 50, 40);
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(pressCancle) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelButton];
    [ToolUtil createLine:70+99*Height superView:self.view lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];

   
    
    UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 49, WindowWidth, 49)];
    downView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:downView];
    [ToolUtil createLine:0 superView:downView lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];
    UIButton *serviceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    serviceButton.frame = CGRectMake(10*Width, 10, 300*Width, 30);
    [serviceButton setTitle:@"微医客服" forState:UIControlStateNormal];
    [serviceButton addTarget:self action:@selector(pressserviceButton) forControlEvents:UIControlEventTouchUpInside];
    serviceButton.backgroundColor = [UIColor blueColor];
    [serviceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [downView addSubview:serviceButton];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(330*Width, 15, 25*Width, 20)];
    imageView.image = [UIImage imageNamed:@"bell"];
    [downView addSubview:imageView];
    
    [self setTable];
     [self.view addSubview:view];
}

- (void)pressserviceButton{
    [self.navigationController pushViewController:[[CustomerServiceViewController alloc] init] animated:YES];
    
    
}
- (void)setTable{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70 +100*Height, WindowWidth, WindowHeight - 119 - 100*Height)];
    self.tableView.delegate= self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self setExtraCellLineHidden:self.tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (seg.selectedSegmentIndex) {
        return 6;
    }
    
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (seg.selectedSegmentIndex) {
        
        if (!flag && i == section) {
            return 2;
        }
        
        return 0;
    }
    
    
    return 10;
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (seg.selectedSegmentIndex) {
        static NSString *cellString = @"TableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString];
        }
        cell.textLabel.text = @"注册登录问题";
        cell.selectionStyle = UITableViewCellAccessoryNone;
        cell.backgroundColor = [UIColor colorWithRed:237.7/255. green:236.4/255. blue:238.9/255. alpha:1.0];
        return cell;
    }
    
    static NSString *string = @"HotProblemTableViewCell";
    
    HotProblemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    
    if (!cell) {
        cell = [[HotProblemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    cell.selectionStyle = UITableViewCellAccessoryNone;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (seg.selectedSegmentIndex) {
        return 50*Height;
    }
    
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (seg.selectedSegmentIndex) {
        return 40;
    }
    
    
    return 55;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50*Height)];
    headerV.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, WindowWidth, 50*Height);
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 800+section;
    [headerV addSubview:button];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20*Height, 0, 300*Width, 49*Height)];
    
    label.text = arr[section];
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor blackColor];
    [button addSubview:label];
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(WindowWidth - 20*Width, 20*Height, 15*Width, 10*Height)];
    
    
    
    imagev.image = [UIImage imageNamed:@"arrow_bottom"];
    if (!flag && i == section && seg.selectedSegmentIndex == 1) {
        imagev.image = [UIImage imageNamed:@"arrow_up"];
    }
    [button addSubview:imagev];
    [ToolUtil createLine:49*Height superView:button lift:0 width:WindowWidth hight:1*Height color:GRAYCOLOR];
    
    return headerV;
}

- (void)pressButton:(UIButton*)button{
    flag = !flag;
    i = button.tag - 800;
    
    [self.tableView reloadData];
   
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.navigationController pushViewController:[[ProblemDetailsViewController alloc] init] animated:YES];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    //self.navigationController.navigationBar.hidden = YES;
    [view addSubview:searchBar];
    searchBar.frame = CGRectMake(10, 69, WindowWidth - 70, 40);
    
    view.hidden = NO;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    return YES;
}

- (void)changeSeg{
    [self.tableView reloadData];
}

- (void)pressCancle{
    searchBar1.frame = CGRectMake(5, 70+50*Height, WindowWidth - 10, 40);
    [self.view addSubview:searchBar1];
    view.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    [searchBar1 resignFirstResponder];
    searchBar1.searchBarStyle = UISearchBarStyleMinimal;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
